const MiniCssExtractPlugin = require('mini-css-extract-plugin')
import paths from './path'
const getPath = paths.getPath

const isBuild = process.env.NODE_ENV === 'production'

// AntDesignThemePlugin 导出color.less的主题变量
let themeVariables: Array<string>
try {
  themeVariables = Object.keys(require(getPath('src/style/antd/themeVars'))).map(key => key)
} catch (e) {
  themeVariables = []
}

// antd 主题
let antdModifyVars
try {
  antdModifyVars = require(getPath('src/style/antd/antd'))
} catch (e) {
  antdModifyVars = {}
}

// const tsLoader = {
//   test: /\.(ts|js)x?$/,
//   exclude: /node_modules/,
//   use: [
//     {
//       loader: "babel-loader",
//       options: {
//         presets: [
//           "@babel/preset-env",
//           "@babel/preset-react",
//           "@babel/preset-typescript",
//         ],
//         plugins: [
//           [
//             "@babel/plugin-transform-runtime",
//             {
//               "regenerator": true
//             }
//           ],
//           "@babel/plugin-syntax-dynamic-import",
//           ['import', {
//             'libraryName': 'antd',
//             'style': true
//           }, 'antd']
//         ]
//       },
//     }
//   ]
// }

const tsLoader = {
  test: /(?<!\.d)\.tsx?$/,
  use: ['ts-loader'],
  exclude: /node_modules/,
}

const imageLoader = {
  test: /\.(gif|png|jpe?g|svg)$/i,
  use: [
    {
      loader: 'file-loader',
      options: {
        name: '[name].[contenthash].[ext]',
        outputPath: 'images/',
        esModule: false
      }
    }
  ],
  exclude: paths.MATCH_NODE_MODULES,
  include: paths.PATH_SRC
}

const pdfLoader = {
  test: /\.(pdf)$/,
  use: [
    {
      loader: 'file-loader',
      options: {
        name: '[name].[contenthash].[ext]',
        outputPath: 'pdf/',
        esModule: false
      }
    }
  ]
}

const fontLoader = {
  test: /\.(eot|ttf|woff|woff2)$/i,
  use: [
    {
      loader: 'file-loader',
      options: {
        name: '[name].[contenthash].[ext]',
        outputPath: 'fonts/',
        esModule: false
      }
    }
  ]
}

const videoLoader = {
  test: /\.(mov|mp4|webm)$/,
  use: [
    {
      loader: 'file-loader',
      options: {
        name: '[name].[contenthash].[ext]',
        outputPath: 'videos/',
        esModule: false
      }
    }
  ]
}

const markdownLoader = {
  test: /\.(md)$/,
  use: [
    {
      loader: 'file-loader',
      options: {
        name: '[name].[contenthash].[ext]',
        outputPath: 'md/',
        esModule: false
      }
    }
  ]
}

const cssLoader = {
  test: /\.css$/,
  use: [
    {
      loader: MiniCssExtractPlugin.loader,
    },
    {
      loader: 'css-loader',
      options: {
        modules: false,
        importLoaders: 1,
        sourceMap: !isBuild
      }
    }
  ]
}

const antdModifyVarsLoader = {
  test: /(?<!\.m)\.less$/,
  use: [
    {
      loader: MiniCssExtractPlugin.loader,
      options: {
        publicPath: ''
      }
    },
    {
      loader: 'css-loader',
      options: {
        modules: false,
        importLoaders: 1,
        sourceMap: !isBuild
      }
    },
    {
      loader: 'less-loader',
      options: {
        lessOptions: {
          modifyVars: antdModifyVars,
          javascriptEnabled: true
        }
      }
    }
  ]
}

const lessLoader = {
  test: /\.m\.less$/,
  exclude: paths.MATCH_NODE_MODULES,
  use: [
    {
      loader: MiniCssExtractPlugin.loader,
      options: {
        publicPath: ''
      }
    },
    {
      loader: 'css-loader',
      options: {
        modules: {
          mode: 'local',
          localIdentName: '[local]___[hash:base64:5]'
        },
        importLoaders: 1,
        sourceMap: !isBuild
      }
    },
    {
      loader: 'less-loader',
      options: {
        lessOptions: {
          javascriptEnabled: true
        }
      }
    }
  ]
}

export default [tsLoader, imageLoader, lessLoader, antdModifyVarsLoader,fontLoader];